import { defineStore } from 'pinia'

export const usePostStore = defineStore('posts-store', {
  /* Data 准备数据*/
  state(){
    return {
      courses: [],
      chapters: [],
      sections: [],
      videoResources: [],
    }
  },
  actions: {
    addCourse(course){
      const newCourses = {
        title: course.title,
        description: course.description,
      };
      fetch('http://localhost:8080/add-courses',{
        method: 'POST',
        headers:{'content-type': 'application/json'},
        body: JSON.stringify(newCourses)
      })
        .then(() => this.getCourses())
        .catch(e => console.log(e))
    },
    getCourses(){
      fetch("http://localhost:8080/list-courses")
        .then(res => res.json())
        .then((data) => {this.courses = data.courses})
    },
    getChapters(){
      fetch(`http://localhost:8080/list-chapters`)
        .then(res => res.json())
        .then((data) => {this.chapters = data.chapters})
    },
    getSections(){
      fetch(`http://localhost:8080/list-sections`)
        .then(res => res.json())
        .then((data) => {this.sections = data.sections})
    },
    getVideoResources(sectionId){
      fetch(`http://localhost:8080/list-video_resources/${sectionId}`)
        .then(res => res.json())
        .then((data) => {this.videoResources = data.videoResources})
    },

  },
  getters:{
    getPost(){

    }
  }

})
